package org.jiuling.aliyun.sms.factory.service.impl;

import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teautil.models.RuntimeOptions;
import org.jiuling.aliyun.sms.config.AliYunSmsConfig;
import org.jiuling.aliyun.sms.convert.*;
import org.jiuling.aliyun.sms.enums.TemplateTypeEnum;
import org.jiuling.aliyun.sms.exception.AliYunSMSRuntimeException;
import org.jiuling.aliyun.sms.factory.AliYunSmsSynchronization;
import org.jiuling.aliyun.sms.factory.service.AliYunTemplateManagerService;
import org.jiuling.aliyun.sms.factory.service.verify.AliYunTemplateManagerVerify;
import org.jiuling.aliyun.sms.model.*;

/**
 * 阿里云同步模板消息Service实现类
 * @author 紫灵
 * @date 2023年02月12日 21时44分13秒
 */
public class AliYunSyncTemplateManagerServiceImpl extends AliYunSmsSynchronization implements AliYunTemplateManagerService {

    public AliYunSyncTemplateManagerServiceImpl(AliYunSmsConfig aliYunSmsConfig) {
        super(aliYunSmsConfig);
    }

    @Override
    public AddSmsTemplateResponseModel addSmsTemplate(TemplateTypeEnum templateType, String templateName, String templateContent, String remark) {
        AliYunTemplateManagerVerify.addSmsTemplateParam(templateType, templateName, templateContent, remark);
        AddSmsTemplateRequest addSmsTemplateRequest = new AddSmsTemplateRequest()
                .setTemplateType(templateType.getValue())
                .setTemplateName(templateName)
                .setTemplateContent(templateContent)
                .setRemark(remark);
        RuntimeOptions runtimeOptions = new RuntimeOptions();
        try {
            AddSmsTemplateResponse addSmsTemplateResponse = create().addSmsTemplateWithOptions(addSmsTemplateRequest, runtimeOptions);
            return AddSmsTemplateResponseConvert.bodyToModel(addSmsTemplateResponse.getBody());
        } catch (Exception e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }

    @Override
    public DeleteSmsTemplateResponseModel deleteSmsTemplate(String templateCode) {
        AliYunTemplateManagerVerify.deleteSmsTemplateParam(templateCode);
        DeleteSmsTemplateRequest deleteSmsTemplateRequest = new DeleteSmsTemplateRequest().setTemplateCode(templateCode);
        try {
            return DeleteSmsTemplateResponseConvert.bodyToModel(create().
                    deleteSmsTemplate(deleteSmsTemplateRequest).getBody());
        } catch (Exception e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }

    @Override
    public ModifySmsTemplateResponseModel modifySmsTemplate(TemplateTypeEnum templateType, String templateName, String templateCode, String templateContent, String remark) {
        AliYunTemplateManagerVerify.modifySmsTemplateParam(templateType, templateName, templateCode, templateContent, remark);
        ModifySmsTemplateRequest modifySmsTemplateRequest = new ModifySmsTemplateRequest()
                .setTemplateType(templateType.getValue())
                .setTemplateName(templateName)
                .setTemplateCode(templateCode)
                .setTemplateContent(templateContent)
                .setRemark(remark);
        try {
            return ModifySmsTemplateResponseConvert.bodyToModel(create().
                    modifySmsTemplate(modifySmsTemplateRequest).getBody());
        } catch (Exception e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }

    @Override
    public QuerySmsTemplateListResponseModel querySmsTemplateList(int pageNum, int pageSize) {
        QuerySmsTemplateListRequest querySmsTemplateListRequest = new QuerySmsTemplateListRequest()
                .setPageIndex(pageNum)
                .setPageSize(pageSize);
        try {
            return QuerySmsTemplateListResponseConvert.bodyToModel(create()
                    .querySmsTemplateList(querySmsTemplateListRequest).getBody());
        } catch (Exception e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }

    @Override
    public QuerySmsTemplateResponseModel querySmsTemplate(String templateCode) {
        AliYunTemplateManagerVerify.querySmsTemplateParam(templateCode);
        QuerySmsTemplateRequest querySmsTemplateRequest = new QuerySmsTemplateRequest()
                .setTemplateCode(templateCode);
        try {
            QuerySmsTemplateResponse querySmsTemplateResponse = create().querySmsTemplate(querySmsTemplateRequest);
            return QuerySmsTemplateResponseConvert.bodyToModel(querySmsTemplateResponse.getBody());
        } catch (Exception e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }
}
